package cn.edu.sdjzu.xg2106.service;

import cn.edu.sdjzu.xg2106.dao.UniversityDao;
import cn.edu.sdjzu.xg2106.domain.University;
import cn.edu.sdjzu.xg2106.exception.BysjException;
import cn.edu.sdjzu.xg2106.util.Condition;
import cn.edu.sdjzu.xg2106.util.JdbcHelper;
import cn.edu.sdjzu.xg2106.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class UniversityService {
    public UniversityDao universityDao =UniversityDao.getInstance();
    public static UniversityService universityService;

    public static UniversityService getInstance(){
        if(universityService == null){
            universityService = new UniversityService();
        }
        return universityService;
    }


    public Collection<University> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException {
        Connection connection= JdbcHelper.getConn();
        Collection<University>universities=universityDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return universities;
    }


    public University find(int id) throws SQLException {
        University university = universityDao.find(id);
        return university;
    }
    public void delete(long id) throws SQLException {
        universityDao.delete(id);
    }
    public boolean add(University university) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        boolean uni = universityDao.add(connection,university);
        JdbcHelper.close(connection);
        return uni;
    }

    public boolean update(University university) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        boolean uni1 = universityDao.update(connection,university);
        JdbcHelper.close(connection);
        return uni1;
    }
}
